package com.zac.mock;

import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertTrue;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.junit.Before;
import org.junit.Test;

/**
 * Test case for LoginService.
 */
public class LoginServiceTest {

	private LoginServiceImpl service;
	private UserDAO mockDao;

	/**
	 * setUp overrides the default, empty implementation provided by JUnit's
	 * TestCase. We will use it to instantiate our required objects so that we
	 * get a clean copy for each test.
	 */
	@Before
	public void setUp() {
		service = new LoginServiceImpl();
		mockDao = createStrictMock(UserDAO.class);
		service.setUserDAO(mockDao);
	}

	/**
	 * This method will test the "rosy" scenario of passing a valid username and
	 * password and retrieveing the user. Once the user is returned to the
	 * service, the service will return true to the caller.
	 */
	@Test
	public void testRosyScenario() {
		User results = new User();
		String userName = "testUserName";
		String password = "testPassword";
		MessageDigest md5 = null;
		String passwordHash = "";
		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.update(password.getBytes());
			passwordHash = new String(md5.digest());
		} catch (NoSuchAlgorithmException e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		expect(
				mockDao.loadByUsernameAndPassword(eq(userName),
						eq(passwordHash))).andReturn(results);

		replay(mockDao);
		assertTrue(service.login(userName, password));
		verify(mockDao);
	}
}
